# -*- coding: utf-8 -*-
# 创建时间：2021/7/16 17:17
from bs4 import BeautifulSoup
import requests
import time, math, random, json
import pymysql
import re
import PySimpleGUI as sg
import os
from hhy import FileUtil,DateUtil,ToolUtil,HttpUtil
__author__ = 'LuckyHhy'
header = {
    'user-agent': HttpUtil.AgentRandom()
}


# 获取所有小说的mp3
def get_details_urls(url):
    url_list = []
    res = requests.get(url=url, headers=header)
    res.encoding = 'gbk'
    soup = BeautifulSoup(res.text, 'html.parser')
    # 获取小说名字
    name = soup.select(".red12")[0].strong.text
    # print(name)
    if not os.path.exists(name):
        os.makedirs(name)

    div_list_a = soup.select("div.list a")
    # print(div_list_a)
    for item in div_list_a:
        url_list.append({'name': item.string, 'href': 'https://www.tingchina.com/yousheng/{}'.format(item['href'])})

    return name, url_list


# 通过播放地址获取完整地址
def get_mp3_key_by_url(url):
    response = requests.get(url, headers=header)
    response.encoding = 'gbk'
    soup = BeautifulSoup(response.text, 'html.parser')
    script = soup.select("script")[-1].string
    # 匹配出
    fileUrl_search = re.search('fileUrl= "(.*?)";', script, re.S)
    # print(fileUrl_search)
    if fileUrl_search:
        urlk = 'https://t3344.tingchina.com' + fileUrl_search.group(1)
        # print(urlk)
        # 获取key
        get_key_url = 'https://img.tingchina.com/play/h5_jsonp.asp?{}'.format(str(random.random()))
        header['referer'] = url
        res = requests.get(get_key_url, headers=header)
        # 匹配key
        matched = re.search('(key=.*?)";', res.text, re.S)
        #print(matched)
        if matched:
            tmp = matched.group(1)
            # print(tmp)
            return urlk + '?key=' + tmp[len(tmp) - 42:]  # key
    else:
        return ''


# 开始爬取
def paqu(url):
    dir, url_list = get_details_urls(url)
    i=0
    for item in url_list:
        audio_url = get_mp3_key_by_url(item['href'])
        if audio_url:
            if i>50:
                break
            i+=1
            header['referer'] = item['href']
            r = requests.get(audio_url, headers=header, stream=True)
            with open(os.path.join(dir, item['name']), 'ab') as f:
                f.write(r.content)
                f.flush()




def main():
    #url = 'https://www.tingchina.com/yousheng/disp_30907.htm'
    # get_details_urls(url)
    # url ='https://www.tingchina.com/yousheng/30907/play_30907_1.htm'
    sg.change_look_and_feel("GreenMono")
    text = sg.Text("请输入浏览器书页的地址：")
    textinput = sg.InputText()
    bt = sg.Button('开启爬取')
    cbt = sg.Button('取消')
    layout = [[text, textinput], [bt,cbt]]
    window = sg.Window('爬取有声小说', layout)
    while True:
        event, values = window.read()
        if event in (None, '取消'):
            break  # 相当于关闭界面

        paqu(values[0])
        #print(f'Event: {event}')
        #print(values[0])



    window.close()
    print("爬取完成")


if __name__ == '__main__':
    main()
